package com.ruoyi.system.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.domain.dto.VehicleVideoDTO;
import com.ruoyi.system.domain.ZcVehicle;
import com.ruoyi.system.domain.ZcVehicleVideo;
import com.ruoyi.system.mapper.ZcVehicleVideoMapper;
import com.ruoyi.system.service.IZcVehicleVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ZcVehicleVideoServiceImpl implements IZcVehicleVideoService {

    @Autowired
    private ZcVehicleVideoMapper videoMapper;

    @Override
    public ZcVehicleVideo getByUniqueId(String checkDate, String uniqueId) {
        ZcVehicleVideo zcVehicleVideo = new ZcVehicleVideo();
        zcVehicleVideo.setTableName(getTableName(checkDate));
        zcVehicleVideo.setUniqueId(uniqueId);
        return videoMapper.getByUniqueId(zcVehicleVideo);
    }

    @Override
    public List<ZcVehicleVideo> getUploadList(String checkDate) {
        ZcVehicleVideo zcVehicleVideo = new ZcVehicleVideo();
        zcVehicleVideo.setCheckDate(checkDate);
        zcVehicleVideo.setTableName(getTableName(checkDate));
        return videoMapper.getUploadList(zcVehicleVideo);
    }

    @Override
    public String getVideoName(String checkNumber, String laneNo) {
        String videoName = checkNumber + "-" + laneNo;
        return videoName;
    }

    @Override
    public String getVideoUrl(String videoName, Date checkTime) {
        String videoUrl = "D:/data/video/" + DateUtil.format(checkTime, "yyyy/MM/dd") + "/" + videoName + ".mp4";
        return videoUrl;
    }

    @Override
    public void updateUpload(String checkNumber) {
        ZcVehicleVideo zcVehicleVideo = new ZcVehicleVideo();
        zcVehicleVideo.setTableName("zc_vehicle_video_" + StrUtil.subPre(checkNumber, 6));
        zcVehicleVideo.setCheckNumber(checkNumber);
        videoMapper.updateUpload(zcVehicleVideo);
    }

    @Override
    public void saveUpload(ZcVehicle zcVehicle) {
        ZcVehicleVideo zcVehicleVideo = this.getByUniqueId(zcVehicle.getCheckDate(), zcVehicle.getUniqueId());
        if (zcVehicleVideo == null) {
            zcVehicleVideo = new ZcVehicleVideo();
            zcVehicleVideo.setUniqueId(zcVehicle.getUniqueId());
            zcVehicleVideo.setCheckNumber(zcVehicle.getCheckNumber());
            zcVehicleVideo.setCheckTime(zcVehicle.getCheckTime());
            zcVehicleVideo.setVideoName(this.getVideoName(zcVehicle.getCheckNumber(), zcVehicle.getLaneNo()));
            zcVehicleVideo.setVideoUrl(this.getVideoUrl(zcVehicleVideo.getVideoName(), zcVehicleVideo.getCheckTime()));
            zcVehicleVideo.setCreateDate(new Date());
        }
        zcVehicleVideo.setLaneNo(zcVehicle.getLaneNo());
        zcVehicleVideo.setPlateNo(zcVehicle.getPlateNo());
        zcVehicleVideo.setPlateColor(zcVehicle.getPlateColor());
        zcVehicleVideo.setCheckDate(zcVehicle.getCheckDate());
        zcVehicleVideo.setDelFlag("2");
        zcVehicleVideo.setRemarks(this.getRemarks(zcVehicleVideo));
        Long id = zcVehicleVideo.getId();
        if (id == null) {
            this.insert(zcVehicleVideo);
        } else {
            this.update(zcVehicleVideo);
        }
    }

    @Override
    public void update(ZcVehicleVideo zcVehicleVideo) {
        String checkDate = zcVehicleVideo.getCheckDate();
        zcVehicleVideo.setTableName(getTableName(checkDate));
        videoMapper.update(zcVehicleVideo);
    }

    @Override
    public void insert(ZcVehicleVideo zcVehicleVideo) {
        String checkDate = zcVehicleVideo.getCheckDate();
        zcVehicleVideo.setTableName(getTableName(checkDate));
        videoMapper.insert(zcVehicleVideo);
    }

    private String getRemarks(ZcVehicleVideo zcVehicleVideo) {
        VehicleVideoDTO dto = new VehicleVideoDTO();
        dto.setUniqueId(IdUtil.simpleUUID());
        dto.setDataId(zcVehicleVideo.getUniqueId());
        dto.setCheckTime(zcVehicleVideo.getCheckTime());
        dto.setCheckNo(zcVehicleVideo.getCheckNumber());
        dto.setLaneNo(zcVehicleVideo.getLaneNo());
        dto.setVehicleNo(zcVehicleVideo.getPlateNo());
        dto.setPlateColor(zcVehicleVideo.getPlateColor());
        dto.setFileName(zcVehicleVideo.getVideoName());
        dto.setFileUrl(zcVehicleVideo.getVideoUrl());
        return JSONUtil.toJsonStr(dto);
    }

    private String getTableName(String checkDate) {
        String yyyyMM = StrUtil.replace(checkDate, "-", "");
        yyyyMM = StrUtil.subPre(yyyyMM, 6);
        return "zc_vehicle_video_" + yyyyMM;
    }
}
